import { MagicWeaponBase } from "./MagicWeaponBase";
import { GImageBitmap } from "../../../../com/mui/controls/GImageBitmap";
import { ImagesConst } from "../../resource/ImagesConst";
import { ResFileConst } from "../../resource/ResFileConst";
import { ToolTipsManager } from "../../../../com/mui/manager/ToolTipsManager";
import { MagicWeaponBestShowToolTip } from "../common/tooltip/MagicWeaponBestShowToolTip";
import { MagicWeaponConfig } from "../../resource/MagicWeaponConfig";
import { ObjCreate } from "../common/ObjCreate";
import { Language } from "../../../../extend/language/Language";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { ScrollPolicy } from "../../../../fl/controls/ScrollPolicy";
import { IdentifyItemRenderer } from "./renderer/IdentifyItemRenderer";
import { ListEvent } from "../../../../fl/events/ListEvent";
import { AutoGuideClickChildNames } from "../common/guide/AutoGuideClickChildNames";
import { WuXingQualityBuyPanel } from "../wuxing/WuXingQualityBuyPanel";
import { ECategory } from "../../../../Message/Public/ECategory";
import { EProp } from "../../../../Message/Public/EProp";
import { LoaderHelp } from "../../../common/display/LoaderHelp";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { Cache } from "../../cache/Cache";
import { EBind } from "../../../../Message/Public/EBind";
import { MagicWeaponItemData } from "./MagicWeaponItemData";
import { MsgManager } from "../../manager/MsgManager";
import { IdentifyVo } from "./data/IdentifyVo";
import { EPlayerItemPosType } from "../../../../Message/Public/EPlayerItemPosType";
import { QuickBuyWindow } from "../common/QuickBuyWindow";
import { DataProvider } from "../../../../fl/data/DataProvider";
import { ItemData } from "../../resource/info/ItemData";
import { SPlayerItem } from "../../../../Message/Public/SPlayerItem";
import { GTileList } from "../../../../com/mui/controls/GTileList";
import { GButton } from "../../../../com/mui/controls/GButton";
import { GCheckBox } from "../../../../com/mui/controls/GCheckBox";
import { TextBox } from "../pack/TextBox";
type int = number;
//class MagicWeaponIdentifyPanel
    
    export  class MagicWeaponIdentifyPanel extends MagicWeaponBase
    {
        constructor()
        {
            super();
            panelType = MagicWeaponBase.IDENTIFY_PANEL;
            this.init();
            return;
        }

        /* internal  */init(): void
        {
            var loc1=new GImageBitmap(ImagesConst.MagicWeaponBg, 43, 188);
            this.addChild(loc1);
            var loc2=this.createBestWeaponBtn(90, 173, ResFileConst.BlueMagicWeaponBtn);
            ToolTipsManager.register(loc2, MagicWeaponBestShowToolTip, MagicWeaponConfig.instance.getMagicWeaponBestShow(3));
            var loc3=this.createBestWeaponBtn(162, 173, ResFileConst.PurpleMagicWeaponBtn);
            ToolTipsManager.register(loc3, MagicWeaponBestShowToolTip, MagicWeaponConfig.instance.getMagicWeaponBestShow(4));
            var loc4=this.createBestWeaponBtn(242, 173, ResFileConst.OrangeMagicWeaponBtn);
            ToolTipsManager.register(loc4, MagicWeaponBestShowToolTip, MagicWeaponConfig.instance.getMagicWeaponBestShow(5));
            var loc5=this.createBestWeaponBtn(316, 173, ResFileConst.DullYellowMagicWeaponBtn);
            ToolTipsManager.register(loc5, MagicWeaponBestShowToolTip, MagicWeaponConfig.instance.getMagicWeaponBestShow(6));
            ObjCreate.createGTextField(Language.getStringByParam(27695, 100), 120, 210, 60, 20, this, GlobalStyle.textFormat6);
            ObjCreate.createGTextField(Language.getStringByParam(27695, 150), 192, 210, 60, 20, this, GlobalStyle.textFormat6);
            ObjCreate.createGTextField(Language.getStringByParam(27695, 230), 270, 210, 60, 20, this, GlobalStyle.textFormat6);
            ObjCreate.createGTextField(Language.getStringByParam(27695, 350), 346, 210, 60, 20, this, GlobalStyle.textFormat6);
            this._operateMaterialList = ObjCreate.createTileList(38, 226, 390, 260, this);
            this._operateMaterialList.scrollPolicy = ScrollPolicy.OFF;
            this._operateMaterialList.columnWidth = 76;
            this._operateMaterialList.rowHeight = 88;
            this._operateMaterialList.setStyle("cellRenderer", IdentifyItemRenderer);
            this._operateMaterialList.addEventListener(ListEvent.ITEM_CLICK, this.onCancelItemHandler);
            operateMaterialList = this._operateMaterialList;
            this.operateMaterialData = [];
            this._onekeyAddBtn = ObjCreate.createGButton(Language.getString(27659), 135, 398, 72, 28, this, "ButtonRed");
            this._onekeyAddBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onOnekeyAddHandler);
            this._identifyBtn = ObjCreate.createGButton(Language.getString(27660), 230, 398, 72, 28, this, "ButtonRed");
            this._identifyBtn.name = AutoGuideClickChildNames.MagicWeapon_Identify2;
            this._identifyBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onIdentifyHandler);
            ObjCreate.createBg(31, 434, 380, 2, this, ImagesConst.SplitLine);
            ObjCreate.createBg(26, 437, 185, 46, this);
            this._buyPanel = new WuXingQualityBuyPanel(ECategory._ECategoryProp, EProp._EPropAmuletIdentifyStone, false, false, false);
            ObjCreate.setObjAttri(this._buyPanel, 29, 420, -1, -1, this);
            this._buyPanel.updateNeedCount(0);
            this._autoBuyMateria = ObjCreate.createCheckBox(Language.getString(27664), 220, 435, 100, 28, this, "GRadioButton");
            this._priorUseBindMateria = ObjCreate.createCheckBox(Language.getString(27665), 220, 456, 160, 28, this, "GRadioButton");
            ObjCreate.createGTextField(Language.getString(27666), 308, 406, 70, 20, this, GlobalStyle.textFormat2);
            this._spendText = ObjCreate.createTextBox("", 340, 406, 70, 20, this, GlobalStyle.textFormatWhite);
            ObjCreate.createBitmap(ImagesConst.Tongqian, 390, 407, this);
            var loc6;
            (loc6 = ObjCreate.createImgBtn(ImagesConst.RuleExplain, 353, 450, 60, 23, this, "Button")).gbutton.toolTipData = Language.getString(27677);
            loc6.gbutton.addEventListener(flash.events.MouseEvent.CLICK, this.clickHelpHandler);
            LoaderHelp.addResCallBack(ResFileConst.magicWeapon, this.onLoadResHandler);
            this.addEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.clean);
            return;
        }

        /* internal  */clickHelpHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.MagicWeaponShowPageGuide));
            return;
        }

        /* internal  */onLoadResHandler(): void
        {
            var loc1=GlobalClass.getBitmap(ImagesConst.MagicWeaponBestShow);
            ObjCreate.setObjAttri(loc1, 40, 180, -1, -1, this);
            return;
        }

        /* internal  */clean(arg1: Object /* flash.events.Event */=null): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=_materialData;
            for(loc1 of loc3) 
            {
                if (!(loc1 && loc1.isSelect)) 
                {
                    continue;
                }
                loc1.isSelect = false;
            }
            this.operateMaterialData = [];
            if (arg1 && pageSelecter) 
            {
                restPage();
            }
            return;
        }

        /* internal  */createBestWeaponBtn(arg1: int, arg2: int, arg3: string): Object /* flash.display.Sprite */
        {
            var loc1=new Object /* flash.display.Sprite */();
            ObjCreate.setObjAttri(loc1, arg1, arg2, -1, -1, this);
            ObjCreate.createGLoadedButton(arg3, 0, 0, 51, 49, loc1);
            return loc1;
        }

        /* internal  */set materialItem(arg1: int)
        {
            if (this._buyPanel) 
            {
                this._buyPanel.updateNeedCount(arg1);
            }
            if (this._identifyBtn) 
            {
                if (arg1 > 0) 
                {
                    this._identifyBtn.toolTipData = Language.getStringByParam(27691, arg1);
                }
                else 
                {
                    this._identifyBtn.toolTipData = "";
                }
            }
            return;
        }

        /* internal  */set spendCoin(arg1: int)
        {
            if (this._spendText) 
            {
                this._spendText.label = arg1.toString();
            }
            return;
        }

        /* internal  */onIdentifyHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=0;
            var loc4=null;
            var loc2=[];
            var loc3=this._operateMaterialList.dataProvider.length;
            var loc5=Cache.instance.pack.backPackCache.getUidListByType(ECategory._ECategoryProp, EProp._EPropAmuletIdentifyStone, int.MAX_VALUE, this._priorUseBindMateria.selected);
            var loc6=Cache.instance.pack.backPackCache.getItemCountByCategoryAndType(ECategory._ECategoryProp, EProp._EPropAmuletIdentifyStone, -1, -1, EBind._EBindNo);
            loc1 = 0;
            while (loc1 < loc3) 
            {
                if ((loc4 = this._operateMaterialList.dataProvider.getItemAt(loc1) as MagicWeaponItemData) && loc4.itemData && loc4.itemData.uid) 
                {
                    if (loc4.itemData.isHasIdentify()) 
                    {
                        return;
                    }
                    loc2.push(loc4.itemData.uid);
                }
                ++loc1;
            }
            if (this.hasMaterial > 0 && !this._autoBuyMateria.selected && loc3 > 0 && loc2.length > 0) 
            {
                this.showQuickBuyWindow();
                return;
            }
            if (loc2.length <= 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(27682));
                return;
            }
            var loc7;
            (loc7 = new IdentifyVo()).posType = EPlayerItemPosType._EPlayerItemPosTypeAmuletBag;
            loc7.amuletUid = loc2;
            loc7.materials = loc5;
            loc7.autoBuy = this._autoBuyMateria.selected ? 1 : 0;
            Dispatcher.dispatchEvent(new DataEvent(EventName.MagicWeaponIdentify, {"vo": loc7}));
            return;
        }

        /* internal  */showQuickBuyWindow(): void
        {
            QuickBuyWindow.instance.updateWindow(ECategory._ECategoryProp, EProp._EPropAmuletIdentifyStone, -1, -1, false);
            QuickBuyWindow.instance.updateDefaultAmount(this.hasMaterial);
            QuickBuyWindow.instance.show();
            return;
        }

        /* internal  */get hasMaterial(): int
        {
            var loc1=Cache.instance.pack.backPackCache.getItemCountByCategoryAndType(ECategory._ECategoryProp, EProp._EPropAmuletIdentifyStone);
            return _needMaterialNum - loc1;
        }

        /* internal  */onOnekeyAddHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.clean();
            this.operateMaterialData = _materialData;
            return;
        }

        /* internal  */onCancelItemHandler(arg1: ListEvent): void
        {
            var loc2=0;
            var loc3=null;
            var loc1=arg1.item as MagicWeaponItemData;
            if (loc1.itemData == null) 
            {
                return;
            }
            if (loc1) 
            {
                loc2 = this._operateMaterialList.dataProvider.getItemIndex(loc1);
                this._operateMaterialList.dataProvider.replaceItemAt(new MagicWeaponItemData(null), loc2);
                this._operateMaterialList.drawNow();
                var loc4=0;
                var loc5=_materialData;
                for(loc3 of loc5) 
                {
                    if (loc3.itemData.uid != loc1.itemData.uid) 
                    {
                        continue;
                    }
                    loc3.isSelect = false;
                    var loc6;
                    _needMaterialNum--;
                    break;
                }
                this.materialItem = _needMaterialNum;
                this.updateWeaponInfo();
                this.spendCoin = _needMaterialNum * 1000;
            }
            return;
        }

        /* internal  */get isSelectItemsUid(): string
        {
            var loc2=null;
            var loc3=0;
            var loc4=0;
            var loc1="";
            if (this._operateMaterialList) 
            {
                loc3 = this._operateMaterialList.dataProvider.length;
                loc4 = 0;
                while (loc4 < loc3) 
                {
                    loc2 = this._operateMaterialList.dataProvider.getItemAt(loc4) as MagicWeaponItemData;
                    if (loc2 && loc2.isSelect && loc2.itemData) 
                    {
                        loc1 = loc1 + ("-" + loc2.itemData.uid);
                    }
                    ++loc4;
                }
            }
            return loc1;
        }

        public /* override */ updateWeaponInfo(): void
        {
            var loc3=null;
            var loc1=Cache.instance.magicWeapon.noIdentifyMagicWeapon;
            var loc2=this.isSelectItemsUid;
            var loc4=0;
            var loc5=loc1;
            for(loc3 of loc5) 
            {
                if (loc2.indexOf(loc3.itemData.uid) == -1) 
                {
                    continue;
                }
                loc3.isSelect = true;
            }
            _materialList.dataProvider = new DataProvider(loc1);
            _materialList.drawNow();
            _materialData = loc1;
            if (pageSelecter) 
            {
                pageSelecter.maxPage = int(Math.ceil(loc1.length / 6));
                currentPage();
            }
            if (this._buyPanel) 
            {
                this._buyPanel.updatePackData();
            }
            return;
        }

        public updateMoney(): void
        {
            return;
        }

        public identifySucceed(arg1: Array<any>): void
        {
            var loc1=0;
            var loc2=0;
            var loc4=null;
            var loc3=[];
            if (arg1) 
            {
                loc2 = arg1.length;
                loc1 = 0;
                while (loc1 < 10) 
                {
                    if (loc1 < loc2) 
                    {
                        (loc4 = new MagicWeaponItemData(new ItemData(arg1[loc1] as SPlayerItem))).name = "MagicWeaponIdentifyName_" + loc1.toString();
                    }
                    else 
                    {
                        loc4 = new MagicWeaponItemData(null);
                    }
                    loc3.push(loc4);
                    ++loc1;
                }
                this._operateMaterialList.dataProvider = new DataProvider(loc3);
                this._operateMaterialList.drawNow();
                _needMaterialNum = 0;
                this.materialItem = _needMaterialNum;
                this.spendCoin = _needMaterialNum * 1000;
            }
            return;
        }

        protected /* override */ set operateMaterialData(arg1: Array<any>)
        {
            super.operateMaterialData = arg1;
            this.materialItem = _needMaterialNum;
            this.updateWeaponInfo();
            this.spendCoin = _needMaterialNum * 1000;
            return;
        }

        protected /* override */ onSelectItemHandler(arg1: ListEvent): void
        {
            var loc1=arg1.item as MagicWeaponItemData;
            if (loc1.isSelect) 
            {
                return;
            }
            super.onSelectItemHandler(arg1);
            this.materialItem = _needMaterialNum;
            this.updateWeaponInfo();
            this.spendCoin = _needMaterialNum * 1000;
            return;
        }

        /* internal */ /* var */_operateMaterialList: GTileList;

        /* internal */ /* var */_onekeyAddBtn: GButton;

        /* internal */ /* var */_identifyBtn: GButton;

        /* internal */ /* var */_autoBuyMateria: GCheckBox;

        /* internal */ /* var */_priorUseBindMateria: GCheckBox;

        /* internal */ /* var */_spendText: TextBox;

        /* internal */ /* var */_buyPanel: WuXingQualityBuyPanel;
    }
